package com.example.mo.allfind.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.mo.allfind.R;
import com.example.mo.allfind.model.User;
import com.example.mo.allfind.util.App;
import com.example.mo.allfind.util.DBHelper;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Bind(R.id.number)
    EditText number;
    @Bind(R.id.password)
    EditText password;
    private Button login, regist;
    ImageView qq, wx, wb;
    SHARE_MEDIA[] platforms = {SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.SINA};
    UMShareAPI umShareAPI;
    int state = 0;
    SharedPreferences sp;
    SharedPreferences.Editor editor;
    DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        setOnclick();
//        User user = new User();
//        user.setPassword("123");
//        user.setNickname("haha");
//        user.setCity("宁波");
//        user.setNumber("123");
//        user.setSex("男");
//        App.user = user;
//        dbHelper.saveUser(user);
        String name = sp.getString("name","");
        String pass = sp.getString("password","");
        App.user = dbHelper.getUserByNameAndPass(name,pass);
        Log.i("aaa",App.user.toString());
        if(sp.getInt("state",0) == 1){
            startActivity(new Intent(MainActivity.this,MapActivity.class));
            finish();
            if(sp.getInt("auto",0) == 1){
                if(sp.getBoolean("disanfang",false)){
                    App.user = new User();
                    App.user.setNickname(sp.getString("nickName",""));
                    App.user.setUrl(sp.getString("url",""));
                    App.user.setSex(sp.getString("sex",""));
                    App.user.setCity(sp.getString("city",""));
                }else{
                    if(!"".equals(name) && !"".equals(pass)){
                        number.setText(name);
                        password.setText(pass);
                        startActivity(new Intent(MainActivity.this,MapActivity.class));
                        finish();
                    }
                }
            }else{
                if(!"".equals(name) && !"".equals(pass)) {
                    number.setText(name);
                    password.setText(pass);
                }
            }
        }
    }
    private void setOnclick() {
        login.setOnClickListener(this);
        regist.setOnClickListener(this);
        qq.setOnClickListener(this);
        wx.setOnClickListener(this);
        wb.setOnClickListener(this);
    }
    private void initView() {
        umShareAPI = UMShareAPI.get(this);
        login = (Button) findViewById(R.id.login);
        regist = (Button) findViewById(R.id.regist);
        qq = (ImageView) findViewById(R.id.qq_login);
        wx = (ImageView) findViewById(R.id.wx_login);
        wb = (ImageView) findViewById(R.id.wb_login);
        sp = this.getSharedPreferences("userInfo",MODE_PRIVATE);
        editor = sp.edit();
        dbHelper = new DBHelper(this);
    }
    /**
     * 授权登录回调接口
     */
    UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //授权开始的回调
        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            //获取授权成功后的用户信息，回调接口里获取数据
            Toast.makeText(MainActivity.this, "授权成功", Toast.LENGTH_SHORT).show();
            String temp = "";
            for (String key : data.keySet()) {
                temp = temp + key + " : " + data.get(key) + "\n";
            }
            umShareAPI.getPlatformInfo(MainActivity.this, platforms[state], new UMAuthListener() {
                @Override
                public void onStart(SHARE_MEDIA share_media) {

                }
                @Override
                public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                    String temp = "";
                    for (String key : map.keySet()) {
                        temp = temp + key + " : " + map.get(key) + "\n";
                    }
                    String name = map.get("screen_name");
                    String city = "";
                    if(state == 2){
                        String[] str = map.get("location").split(" ");
                        city = str[1];
                    }else{
                        city  = map.get("city");
                    }
                    String sex = map.get("gender");
                    String url = map.get("iconurl");
                    App.user = new User();
                    App.user.setNickname(name);
                    App.user.setSex(sex);
                    App.user.setCity(city);
                    App.user.setUrl(url);
                    dbHelper.saveUser(App.user);
                    editor.putInt("state",1);
                    editor.putInt("auto",1);
                    editor.putString("city",city);
                    editor.putString("url",url);
                    editor.putString("nickName",name);
                    editor.putString("sex",sex);
                    editor.putBoolean("disanfang",true);
                    editor.commit();
                    Intent intent = new Intent(MainActivity.this, MapActivity.class);
                    startActivity(intent);
                }
                @Override
                public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

                }

                @Override
                public void onCancel(SHARE_MEDIA share_media, int i) {

                }
            });

        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
        }
    };
//    /**
//     * 授权登录回调接口
//     */
//    UMAuthListener umAuthListener1 = new UMAuthListener() {
//        @Override
//        public void onStart(SHARE_MEDIA platform) {
//            //授权开始的回调
//        }
//        @Override
//        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//            //获取授权成功后的用户信息，回调接口里获取数据
//            String temp = "";
//            for (String key : data.keySet()) {
//                temp = temp + key + " : " + data.get(key) + "\n";
//            }
//            String name = data.get("screen_name");
//            String city = data.get("location");
//            String[] citys = city.split(" ");
//            String sex = data.get("gender");
//            String url = data.get("iconurl");
//            App.user = new User();
//            App.user.setNickname(name);
//            App.user.setSex(sex);
//            App.user.setCity(citys[1]);
//            App.user.setUrl(url);
//            Intent intent = new Intent(MainActivity.this, MapActivity.class);
//            startActivity(intent);
//        }
//
//        @Override
//        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
//            Toast.makeText(MainActivity.this, "授权失败", Toast.LENGTH_SHORT).show();
//        }
//
//        @Override
//        public void onCancel(SHARE_MEDIA platform, int action) {
//            Toast.makeText(MainActivity.this, "授权取消", Toast.LENGTH_SHORT).show();
//        }
//    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                String name = number.getText().toString().trim();
                String pass = password.getText().toString().trim();
                if("".equals(name)){
                    Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if("".equals(pass)){
                    Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                User user = dbHelper.getUserByNameAndPass(name,pass);
//                if(user != null){
////                    App.user = user;
////                    User user = new User();
////                    user.setSex("男");
////                    user.setNumber("123");
////                    user.setCity("宁波");
////                    user.setPassword("123");
////                    user.setNickname("Haha");
//                    App.user = user;
//                    Log.i("aaa",user.getCity()+user.getNumber());
//                    editor.putString("name",name);
//                    editor.putString("password",pass);
//                    editor.putInt("state",1);
//                    editor.putInt("auto",1);
//                    editor.commit();
//                    Intent intent = new Intent(MainActivity.this, MapActivity.class);
//                    startActivity(intent);
//                }else{
//                    Toast.makeText(this,"用户名/密码错误！",Toast.LENGTH_SHORT).show();
//                }
                Toast.makeText(this,"连接服务器失败！",Toast.LENGTH_SHORT).show();
                break;
            case R.id.regist:
                startActivity(new Intent(MainActivity.this, Regist1Activity.class));
                break;
            case R.id.qq_login:
                state = 0;
                umShareAPI.doOauthVerify(MainActivity.this, platforms[state], umAuthListener);
                break;
            case R.id.wx_login:
                Toast.makeText(MainActivity.this, "微信登录还在建设中...", Toast.LENGTH_SHORT).show();
                break;
            case R.id.wb_login:
                state = 2;
                umShareAPI.doOauthVerify(MainActivity.this, platforms[state], umAuthListener);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
